/**
 * Created by qiang on 2017-03-30 .
 */
var service = {
	url: "http://www.51780.com:3005"//接口地址
};

var loading = null;
var loadCount = 0
//执行AJAX请求
service.ajaxRequest = function (params, callback, noNeedLoading) {
	if (!noNeedLoading) {
		loading = weui.loading('加载中');
		loadCount++
	}
	$.ajax({
		url: params.url,
		contentType: "application/json",
		data: JSON.stringify(params.data),
		"Content-Type": "utf-8",
		type: params.method,
		headers: {
			token: sessionStorage.getItem("token")
		},
		async: true,
		dataType: "json",
		success: function (data) {
			if (!noNeedLoading && loading.parentNode != null && loadCount == 1) {
				loading.hide()
			}
			if (!noNeedLoading) {
				loadCount--
			}
			switch (data.code) {
				case 100:
				case 103:
				case 201:
					sessionStorage.removeItem("token");
					common.auth(callback);
					break;
				case 0:
					callback(data.result);
					break;
				default:
					weui.alert(data.message || data.errorInfo);
                    // alert(JSON.stringify(data));
					break;
			}
		},
		error: function (err) {
			// alert(JSON.stringify(err));
			callback(err, null);
		}
	});
};
// 封装POST 请求
service.postRequest = function (url, reqData, callback, noNeedLoading) {
	var param = {
		// url: service.url + url,
		url: url,
		method: "POST",
		data: reqData
	};
	//alert(JSON.stringify(param));
	service.ajaxRequest(param, callback, noNeedLoading)
};

service.fileUpload = function (url, reqData, callback) {
  loading = weui.loading('加载中');
  $.ajax({
    url: url,
    type: "POST",
    headers: {
      token: sessionStorage.getItem("token")
    },
    data: reqData,
    processData: false,
    contentType: false,
    success: function (data) {
      loading.hide()
      switch (data.code) {
        case 100:
        case 103:
        case 201:
          sessionStorage.removeItem("token");
          common.auth(callback);
          break;
        case 0:
          callback(data.result);
          break;
        default:
          weui.alert(data.message)
          break;
      }
    },
    error: function () {
      console.log('Upload error');
    }
  });
}

service.getToken = function (params, callback) {
	service.postRequest("/app/weChat/getToken", params, callback);
};

service.register = function (params, callback) {
	service.postRequest("/app/user/create",params, callback)
}

service.getUserDetail = function (params, callback) {
	service.postRequest("/app/user/detail",params, callback)
}

service.updateUser = function (params, callback) {
	service.postRequest("/app/user/update", params, callback)
}

service.getUserList = function (params, callback) {
	service.postRequest("/app/user/list", params, callback)
}

service.getUnRegisterUserList = function (params, callback) {
	service.postRequest("/app/user/nrList", params, callback)
}

service.getFriendList = function (params, callback) {
	service.postRequest("/app/friend/list", params, callback, true)
}

service.getMakeFriendList = function (params, callback) {
	service.postRequest("/app/makeFriend/list", params, callback)
}

service.addFriend = function (params, callback) {
	service.postRequest("/app/makeFriend/create", params, callback)
}

service.removeMakeFriend = function (params, callback) {
	service.postRequest("/app/makeFriend/delete", params, callback)
}

service.acceptMakeFriend = function (params, callback) {
	service.postRequest("/app/makeFriend/accept", params, callback)
}

service.getBlackList = function (params, callback) {
	service.postRequest("/app/blackList/list", params, callback)
}

service.refreshMakeFriend = function (params, callback) {
	service.postRequest("/app/makeFriend/refresh", params, callback)
}

service.createBlack = function (params, callback) {
	service.postRequest("/app/blackList/create", params, callback)
}

service.createTopic = function (params, callback) {
	service.postRequest("/app/topic/create", params, callback)
}

service.getTopicList = function (params, callback) {
	service.postRequest("/app/topic/list", params, callback)
}

service.getAllTopicList = function (params, callback) {
	service.postRequest("/app/topic/allList", params, callback)
}

service.getChatList = function (params, callback) {
	service.postRequest("/app/chat/list", params, callback, true)
}

service.praiseTopic = function (params, callback) {
	service.postRequest("/app/topic/praise", params, callback)
}

service.cancelPraise = function (params, callback) {
	service.postRequest("/app/topic/cancelPraise", params, callback)
}

service.createComment = function (params, callback) {
	service.postRequest("/app/comment/create", params, callback)
}

service.createChat = function (params, callback) {
	service.postRequest("/app/chat/create", params, callback)
}

service.getRecoedListByCahtNo = function (params, callback) {
	service.postRequest("/app/chat/recordList", params, callback, true)
}

service.deleteBlack = function (params, callback) {
	service.postRequest("/app/blackList/delete", params, callback)
}

service.getRechageOptionList = function (callback) {
	service.postRequest("/app/rechargeOption/list", {}, callback)
}

service.createWXPay = function (params, callback) {
	service.postRequest("/app/weChatPay/create", params, callback)
}

service.getQRCode = function (callback) {
	service.postRequest("/app/weChat/getQRCode", {}, callback)
}

service.getQRCodeImg = function (callback) {
	service.postRequest("/app/weChat/getQRCodeImg", {}, callback)
}

service.getWXConfig = function (params, callback) {
	service.postRequest("/app/weChat/getWXConfig", params, callback)
}

service.evaluateUser = function (params, callback) {
	service.postRequest("/app/user/evaluate", params, callback)
}

service.uploadHeadImage = function (params, callback) {
	service.fileUpload("/app/user/update", params, callback)
}

service.getImage = function (params, callback) {
	service.postRequest("/app/image/getImage", params, callback)
}

service.getCityByLocation = function (params, callback) {
	service.postRequest("/app/location/getLocation", params, callback)
}

service.removeTopic = function (params, callback) {
	service.postRequest("/app/topic/delete", params, callback)
}

service.removeComment = function (params, callback) {
	service.postRequest("/app/comment/delete", params, callback)
}

service.updateOnlineState = function (params, callback) {
	// console.log("http://" + window.location.hostname + ":3009/user/online");
	service.postRequest("http://" + window.location.hostname + ":3009/user/online", params, callback, true)
}
